package top.itaso.app.ui.login

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import android.text.TextUtils
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.text.InputType
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.DrawableCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import top.itaso.app.MainDrawerActivity
import top.itaso.app.R
import top.itaso.app.databinding.ActivityLoginBinding

/**
 * @Description: 用户登录界面
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Gitee https://gitee.com/itsdf07
 * @Date 2024/1/16
 */
class LoginActivity : AppCompatActivity() {
    private lateinit var mViewModel: LoginViewModel
    private lateinit var binding: ActivityLoginBinding

    /**
     *账号输入框
     */
    private lateinit var etUserName: EditText

    /**
     *密码输入框
     */
    private lateinit var etPassword: EditText

    /**
     *短信验证码登录
     */
    private lateinit var btnLoginByMsg: TextView

    /**
     *账号登录
     */
    private lateinit var btnLogin: TextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()

        mViewModel.loginUser.observe(this, Observer { userEntity ->
            if (userEntity == null || userEntity.loginState == 0) {
                Toast.makeText(this@LoginActivity, "登录失败：请检查网络", Toast.LENGTH_SHORT).show()
                return@Observer
            }
            if (userEntity.loginState == 1) {
                Toast.makeText(
                    this@LoginActivity,
                    userEntity.userName + "登陆成功",
                    Toast.LENGTH_SHORT
                ).show()
                startActivity(Intent(this@LoginActivity, MainDrawerActivity::class.java))
                finish()
            }

        })
    }

    private fun initView() {
        etUserName = binding.etInputUsername
        etPassword = binding.etInputPassword
        btnLoginByMsg = binding.btnLoginMsg
        btnLogin = binding.btnLogin

        btnLogin.setOnClickListener {
            val userName = etUserName.text.toString()
            if (TextUtils.isEmpty(userName)) {
                Toast.makeText(this, "请输入用户ID", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val password = etPassword.text.toString();
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            mViewModel.login(userName, password, true)
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }

    /**
     * 切换显示密码
     *
     * @param showPassWord
     */
    fun togglePassword(
        inputEditText: EditText,
        iconLockPassword: ImageView,
        showPassWord: Boolean
    ) {
        val selectionEnd = inputEditText.selectionEnd
        val selectionStart = inputEditText.selectionStart
        inputEditText.inputType = (InputType.TYPE_CLASS_TEXT //输入类型为普通文本
                or if (showPassWord) InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD else  //密码可见
            InputType.TYPE_TEXT_VARIATION_PASSWORD //密码不可见
                )
        inputEditText.setSelection(selectionStart, selectionEnd)
        val colors = resources.getColorStateList(R.color.selector_lock_password)
        if (showPassWord) {
            iconLockPassword.setImageDrawable(
                LoginActivity.tintDrawable(
                    resources.getDrawable(R.mipmap.ic_eye_open),
                    colors
                )
            )
        } else {
            iconLockPassword.setImageDrawable(
                LoginActivity.tintDrawable(
                    resources.getDrawable(R.mipmap.ic_eye_close),
                    colors
                )
            )
        }
    }

    companion object {
        /**
         * 给 drawable 着色
         *
         * @param drawable 需要着色的 drawable 对象
         * @param colors   ColorStateList 对象，代表需要着色的颜色
         * @return
         */
        fun tintDrawable(drawable: Drawable, colors: ColorStateList?): Drawable {
            //这里需要对 drawable 对象执行 mutate() 操作
            //该操作能防止一个屏幕里多次使用同一个图片，对其中一个图片操作时影响其他图片
            //当然，你也可以在getResource().getDrawable()的时候就执行
            val wrappedDrawable = DrawableCompat.wrap(drawable.mutate())
            DrawableCompat.setTintList(wrappedDrawable, colors)
            return wrappedDrawable
        }
    }
}